package com.dongjiantong.web.boss.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.DicData;
import com.dongjiantong.sys.service.SysAreaService;
import com.dongjiantong.user.model.RegionAccount;
import com.dongjiantong.user.service.RegionAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by cao on 2017/11/10.
 * @author
 */
//@RestController
//@RequestMapping("/api/boss/account")
@Slf4j
@OpenBean
public class RegionAccountController {


    @Reference(version = "1.0.0")
    public RegionAccountService regionAccountService;

    @Reference(version = "1.0.0")
    public SysAreaService sysAreaService;

    //@PostMapping("/createRegionAccount")
    @OpenMethod(
            key = "boss.create.regionAccount",
            paramNames = {"provinceId","cityId","regionId","streetId"},
            httpMethods = RequestMethod.POST
    )
    public Response<Boolean> createRegionAccount(Integer provinceId,
                                                  Integer cityId,
                                                  Integer regionId,
                                                  Integer streetId) {
        if (cityId == null || cityId == 0) {
            return Response.fail(HttpCode.BAD_REQUEST, "请选择市");
        }

        if (regionId == null) {
            regionId = 0;
        }
        if (streetId == null) {
            streetId = 0;
        }

        RegionAccount regionAccount = new RegionAccount();

        regionAccount.setProvinceId(provinceId);
        regionAccount.setCityId(cityId);
        regionAccount.setRegionId(regionId);
        regionAccount.setStreetId(streetId);
        regionAccount.setCreatedId(1L);


        String province = sysAreaService.getAreaName(provinceId, 0, 0, 0, 0).getArea_name();
        String city = sysAreaService.getAreaName(provinceId, cityId, 0, 0, 0).getArea_name();
        String region="";
        String street="";

        if (regionId > 0) {
            region = sysAreaService.getAreaName(provinceId, cityId, regionId, 0, 0).getArea_name();
        }
        if (streetId > 0) {
            street = sysAreaService.getAreaName(provinceId, cityId, regionId, streetId, 0).getArea_name();
        }

       Integer result = regionAccountService.createRegionAccount(regionAccount,
               province,city,region,street);

       if(result==0) {
           return Response.fail(HttpCode.INTERNAL_SERVER_ERROR,"发生错误，请重试");
       }

       return Response.ok(result>0);
    }

    //@GetMapping("/regionAccount")
    @OpenMethod(
            key = "boss.regionAccount.list",
            paramNames = {},
            httpMethods = RequestMethod.GET
    )
    public Response<List<Map<String,Object>>> regionAccountList()
    {

        return Response.ok(regionAccountService.regionAccountList());
    }


}
